package com.atxyj.srb.core.service.impl;

import com.atxyj.common.exception.Assert;
import com.atxyj.common.result.ResponseEnum;
import com.atxyj.srb.core.enums.UserBindEnum;
import com.atxyj.srb.core.hfb.FormHelper;
import com.atxyj.srb.core.hfb.HfbConst;
import com.atxyj.srb.core.hfb.RequestHelper;
import com.atxyj.srb.core.mapper.UserBindMapper;
import com.atxyj.srb.core.mapper.UserInfoMapper;
import com.atxyj.srb.core.pojo.entity.UserBind;
import com.atxyj.srb.core.pojo.entity.UserInfo;
import com.atxyj.srb.core.pojo.vo.UserBindVO;
import com.atxyj.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author xyj
 * @since 2021-04-27
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVO userBindVO, Long userId) {

        //不同的userid，相同的身份证。如果存在，则不可以
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("id_card" , userBindVO.getIdCard())
        .ne("user_id" , userId);

        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        //用于判断用户是否曾经填写过绑定
         userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id" , userId);
         userBind = baseMapper.selectOne(userBindQueryWrapper);

        if(userBind == null) {
            // 不存在，则创建用户绑定记录
           userBind = new UserBind();
           BeanUtils.copyProperties(userBind , userBindVO);
           userBind.setUserId(userId);
           userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
           baseMapper.insert(userBind);
         } else {
            //存在则更新
            //相同的userid和身份证，则更新
            BeanUtils.copyProperties(userBindVO, userBind);
            baseMapper.updateById(userBind);
        }

        //组装自动提交表单的参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId" , HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard" , userBindVO.getIdCard());
        paramMap.put("personalName" , userBindVO.getName());
        paramMap.put("bankType" , userBindVO.getBankType());
        paramMap.put("bankNo" , userBindVO.getBankNo());
        paramMap.put("mobile" , userBindVO.getMobile());
        paramMap.put("returnUrl" , HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign" , RequestHelper.getSign(paramMap));
        //生成动态表单字符串
        String formStr =  FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return formStr;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notify(Map<String, Object> paramMap) {
     String bindCode =  (String) paramMap.get("bindCode");
     String agentUserId = (String) paramMap.get("agentUserId");
     //根据userid查询user_bind记录
     QueryWrapper<UserBind> userBindQueryWrapper =  new QueryWrapper<>();
     userBindQueryWrapper.eq("user_id" , agentUserId);

     UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
     userBind.setBindCode(bindCode);
     userBind.setStatus(UserBindEnum.BIND_OK.getStatus());

     //更新用户绑定表
     baseMapper.updateById(userBind);

     //更新用户表
     UserInfo userInfo = userInfoMapper.selectById(agentUserId);
     userInfo.setBindCode(bindCode);
     userInfo.setName(userBind.getName());
     userInfo.setIdCard(userBind.getIdCard());
     userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());

     //目的让尚融宝和远程数据汇付宝表保持一致
     userInfoMapper.updateById(userInfo);


    }
}
